#include "SypEngine.h"

#ifdef CHECKMEM
#include "mmgr.h"
#endif
namespace Syp
{
FontRegistry* FontRegistry::fg = 0;
FT_Library FontRegistry::library = 0;

void FontRegistry::clearFontBuffers()
{
	//Clear font buffer every frame
	std::map<String,Font*>::iterator j = passes.begin();
	while (j!= passes.end())
	{
		j->second->clearBuffer();
		++j;
	}
}

FT_Library& FontRegistry::getFTLibrary()
{
	return library;
}

void FontRegistry::initialise()
{
	if(!fg)
	{
	fg = new FontRegistry();
		if( FT_Init_FreeType( &library ) )
		{
			ErrorLogger::showError("Error initialising FreeType2 library. Fonts may not be displayed.");
			library = 0;
		}

	}	
}
void FontRegistry::release()
{
	if(fg)
	{
		FT_Done_FreeType( library);
		fg->removeAllEntries();
		delete fg;
		fg = 0;
	}
}
void FontRegistry::dumpInfo()
{
ErrorLogger::getLogger()<<"FontRegistry has "<<getNumEntries()<<" Font(s).\n";
	dumpEntries();	
}

FontRegistry& FontRegistry::getRegistry()
{
	return (*fg);
}

bool FontRegistry::addFont(Font* f)
{
	Font* old = getEntry(f->getFontName());
	if(old)
	{
		ErrorLogger::getLogger()<<"Error : Font with a similar name already exists. Font Name : "<<old->getFontName()<<"\n Font was not added\n";
		return false;
	}
	//else attempt to add p
	addEntry(f->getFontName(),f);
			return true;
}
}
